/**
 * 
 */
package com.cqcis.uip.module.salary.oper.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cqcis.uip.module.salary.oper.SalaryConstants;
import com.cqcis.uip.module.salary.oper.dao.ContractDao;
import com.cqcis.uip.module.salary.oper.dao.EmployeDao;
import com.cqcis.uip.module.salary.oper.dao.SalaryCourseTimesDetailDao;
import com.cqcis.uip.module.salary.oper.dao.SalaryCourseTimesManagerDetailDao;
import com.cqcis.uip.module.salary.oper.dao.SalaryMainDao;
import com.cqcis.uip.module.salary.oper.dao.SalaryTCDetailDao;
import com.cqcis.uip.module.salary.oper.dao.SalaryTrusteeshipDetailDao;
import com.cqcis.uip.module.salary.oper.domain.Employe;
import com.cqcis.uip.module.salary.oper.domain.Salary;
import com.cqcis.uip.module.salary.oper.domain.SalaryCourseTimesDetail;
import com.cqcis.uip.module.salary.oper.domain.SalaryCourseTimesManagerDetail;
import com.cqcis.uip.module.salary.oper.domain.SalaryMain;
import com.cqcis.uip.module.salary.oper.domain.SalaryTCDetail;
import com.cqcis.uip.module.salary.oper.domain.SalaryTrusteeshipDetail;
import com.cqcis.uip.module.salary.oper.service.ProcessEmployeService;
import com.cqcis.uip.module.salary.oper.service.ProcessService;

/**
 * @author 李世军
 *
 * 创建于 2012-3-7 下午02:19:24
 */
public class ProcessServiceImpl implements ProcessService {
	@SuppressWarnings("unused")
	private static Log log = LogFactory.getLog(ProcessServiceImpl.class);
	private EmployeDao employeDao ;
	private SalaryMainDao salaryMainDao ;
	private SalaryTCDetailDao salaryTCDetailDao ;
	private ContractDao contractDao ;
	private ProcessEmployeService processAllService ;
	private SalaryCourseTimesDetailDao salaryCourseTimesDetailDao ;
	private SalaryTrusteeshipDetailDao salaryTrusteeshipDetailDao ;
	private SalaryCourseTimesManagerDetailDao salaryCourseTimesManagerDetailDao ;
	
	public void setContractDao(ContractDao contractDao) {
		this.contractDao = contractDao;
	}
	public void setSalaryCourseTimesManagerDetailDao(
			SalaryCourseTimesManagerDetailDao salaryCourseTimesManagerDetailDao) {
		this.salaryCourseTimesManagerDetailDao = salaryCourseTimesManagerDetailDao;
	}
	public void setProcessAllService(ProcessEmployeService processAllService) {
		this.processAllService = processAllService;
	}
	public void setSalaryTrusteeshipDetailDao(
			SalaryTrusteeshipDetailDao salaryTrusteeshipDetailDao) {
		this.salaryTrusteeshipDetailDao = salaryTrusteeshipDetailDao;
	}
	public void setSalaryCourseTimesDetailDao(
			SalaryCourseTimesDetailDao salaryCourseTimesDetailDao) {
		this.salaryCourseTimesDetailDao = salaryCourseTimesDetailDao;
	}
	public void setSalaryTCDetailDao(SalaryTCDetailDao salaryTCDetailDao) {
		this.salaryTCDetailDao = salaryTCDetailDao;
	}
	public void setSalaryMainDao(SalaryMainDao salaryMainDao) {
		this.salaryMainDao = salaryMainDao;
	}
	public void setEmployeDao(EmployeDao employeDao) {
		this.employeDao = employeDao;
	}

	/* (non-Javadoc)
	 * @see com.cqcis.uip.module.salary.oper.service.ProcessService#process()
	 */
	public void doProcess() {
		List<Employe> es = employeDao.query(SalaryConstants.EMPLOYE_STATE_NORMAL) ;
		es.addAll(employeDao.query(SalaryConstants.EMPLOYE_STATE_5)) ;
		List<Salary> salarys = new ArrayList<Salary>() ;
		
		// 所有员工采用最大化工资计算
		for(Employe e : es){
			salarys.add(processAllService.process(e)) ;
		}
		
		
		// 保存
		if(salarys!=null){
			salaryCourseTimesManagerDetailDao.delete() ;
			salaryTrusteeshipDetailDao.delete() ;
			salaryCourseTimesDetailDao.delete() ;
			salaryTCDetailDao.delete() ;
			salaryMainDao.delete() ;
			for(Salary salary : salarys){
				// 主表
				SalaryMain main = salary.getSalaryMain() ;
				salaryMainDao.insert(main) ;
				
				// 合同提成
				List<SalaryTCDetail> details = salary.getSalaryTCDetails() ;
				if(details!=null)
					for(SalaryTCDetail detail : details){
						salaryTCDetailDao.insert(detail) ;
					}
				
				// 教师课时提成
				List<SalaryCourseTimesDetail> courseDetails = salary.getSalaryCourseTimesDetails() ;
				if(courseDetails!=null)
					for(SalaryCourseTimesDetail detail : courseDetails){
						if(detail!=null && detail.getCompany_id()!=0)
							salaryCourseTimesDetailDao.insert(detail) ;
					}
				
				// 学管师,主管课时提成
				List<SalaryCourseTimesManagerDetail> courseDetailsManager = salary.getSalaryCourseTimesManagerDetail() ;
				if(courseDetails!=null)
					for(SalaryCourseTimesManagerDetail detail : courseDetailsManager){
							salaryCourseTimesManagerDetailDao.insert(detail) ;
					}
				
				// 托管提成
				List<SalaryTrusteeshipDetail> trusteeshipDetails = salary.getSalaryTrusteeshipDetails() ;
				if(trusteeshipDetails!=null)
					for(SalaryTrusteeshipDetail detail : trusteeshipDetails){
						salaryTrusteeshipDetailDao.insert(detail) ;
					}
				
				
			}
		}
	}

	
	public void finishContract() {
		contractDao.finishContract() ;
	}
	
}
